package com.martin.fast_frame_compose.page.main

import androidx.lifecycle.ViewModel
import androidx.navigation.NavHostController
import androidx.navigation.NavOptionsBuilder
import com.martin.lib_base.interfaces.ILoading
import com.martin.lib_base.ktx.requireContext
import com.martin.lib_base.model.LoadingModel
import dev.utils.app.ClickUtils
import kotlin.properties.Delegates

class MainViewModel : ViewModel(), ILoading {

    private val loadingModel by lazy {
        LoadingModel(requireContext())
    }

    // 页面数据
    val pageData = mutableMapOf<String, Map<String, Any>>()

    // 导航控制器
    var navController by Delegates.notNull<NavHostController>()

    ///////////////////////////////////////////////////////////////////////////
    // 方法区
    ///////////////////////////////////////////////////////////////////////////

    fun navigateTo(
        route: String,
        data: Map<String, Any> = emptyMap(),
        builder: NavOptionsBuilder.() -> Unit = {}
    ) {
        // 防连点
        if (ClickUtils.isFastDoubleClick()) return

        // 设置页面数据
        pageData[route] = data
        // 导航
        navController.navigate(route, builder)
    }

    override fun showLoading() {
        loadingModel.showLoading()
    }

    override fun dismissLoading() {
        loadingModel.dismissLoading()
    }

}